package com.bms.controller;

import com.bms.constant.Constants;
import com.bms.model.UserInfo;
import com.bms.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(String userName, String password, HttpSession session){
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return "用户名或密码为空!!";
        }
//        if (!"admin".equals(userName) && !"admin".equals(password)){
//            return "密码错误!!";
//        }
        //
        //根据用户名称在数据库中进行查询,如果没有查询到说明用户不存在
        //如果查询到用户信息那么就进行密码校验
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if (userInfo == null){
            return "用户不存在!!";
        }
        //用户存在,密码校验
        if (!userInfo.getPassword().equals(password)){
            return "密码错误!!";
        }
        //正确的情况
        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
        return "";
    }
}
